from machine import ADC

class LM35:
    def __init__(self, pin):
        self.__adc = ADC(0)
        self.__adc.atten(ADC.ATTN_11DB)
        self.__adc.width(ADC.WIDTH_12BIT)

    def read(self):
        value = self.__adc.read()  # 读取ADC值
        # 计算传感器的电压值，假设参考电压是3.3V
        voltage = value * 3.3 / 4095.0  # 电压 = (ADC值 / 最大值) * 参考电压
        # LM35每输出10mV代表1°C，所以电压 / 0.01 = 摄氏度
        celsius = voltage * 100  # LM35输出每10mV = 1°C，100倍转换
        return celsius

if __name__ == "__main__":
    import time
    lm35 = LM35(0)
    while True:
        c = lm35.read()
        print(f"{c} C")
        time.sleep(1)
